package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.annotation.RequireLogin;
import cn.wolfcode.wolf2w.annotation.UserParam;
import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Travel;
import cn.wolfcode.wolf2w.domain.TravelComment;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.query.TravelQuery;
import cn.wolfcode.wolf2w.redis.service.IUserInfonRedisService;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.ITravelCommentService;
import cn.wolfcode.wolf2w.service.ITravelService;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import cn.wolfcode.wolf2w.util.JsonResult;
import cn.wolfcode.wolf2w.utli.UMEditorUploader;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/travels")
@RestController
public class TravelslController {
    @Autowired
    private ITravelService travelService;
    @Autowired
    private IDestinationService destinationService;
    @Autowired
    private ITravelCommentService travelCommentService;

    @GetMapping("/query")
    public Object search(TravelQuery qo) {
        return JsonResult.success(travelService.query(qo));
    }
    @GetMapping("/input")
    public Object inut(String id) {
        List<Destination> destinations = destinationService.list();
        Map<String, Object> mp = new HashMap<>();
        if (StringUtils.hasLength(id)){
            Travel travel = travelService.get(id);
            mp.put("tv",travel);
        }
        mp.put("dests",destinations);
        return JsonResult.success(mp);
    }


    //上传图片
    @RequestMapping("/contentImage")
    public String uploadUEImage(MultipartFile upfile, HttpServletRequest request) throws Exception{
        UMEditorUploader up = new UMEditorUploader(request);
        String[] fileType = {".gif" , ".png" , ".jpg" , ".jpeg" , ".bmp"};
        up.setAllowFiles(fileType);
        up.setMaxSize(10000); //单位KB
        up.upload(upfile);

        String callback = request.getParameter("callback");
        String result = "{\"name\":\""+ up.getFileName() +"\", \"originalName\": \""+ up.getOriginalName() +"\", \"size\": "+ up.getSize()
                +", \"state\": \""+ up.getState() +"\", \"type\": \""+ up.getType() +"\", \"url\": \""+ up.getUrl() +"\"}";
        result = result.replaceAll( "\\\\", "\\\\" );
        if(callback == null ){
            return result ;
        }else{
            return "<script>"+ callback +"(" + result + ")</script>";
        }
    }


    @PostMapping("/saveOrUpdate")
    @RequireLogin
    public Object saveOrUpdate(Travel travel,@UserParam UserInfo user) {
        //用上自定义参数解析器
        travel.setUserId(user.getId());

        travelService.saveOrUpdate(travel);
        return JsonResult.success(travel.getUserId());
    }

    @GetMapping("/detail")
    public Object detail(String id) {
        Travel travel = travelService.get(id);
        return JsonResult.success(travel);
    }
    //测试自定义参数解析器,@Userparam 区分
    @GetMapping("/user")
    public Object user( /*@UserParam*/ UserInfo userInfo) {
        System.out.println(userInfo);
        return JsonResult.success(userInfo);
    }

    @PostMapping("/commentAdd")
    @RequireLogin
    public Object commentAdd(TravelComment travelComment, @UserParam UserInfo user) {
        BeanUtils.copyProperties(user,travelComment);
        travelComment.setUserId(user.getId());

        travelCommentService.save(travelComment);

        return JsonResult.success();
    }

    @GetMapping("/comments")
    public Object comments( String travelId) {

        return JsonResult.success(travelCommentService.qureyByTravelId(travelId));
    }

}
